(function() {
  var express, iced, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    if (!req.session.host) {
      return next(new Error('请先选择待配置的主机'));
    }
    return res.redirect('/i/091/1');
  });

  router.get('/:mode', function(req, res, next) {
    var client, done, err, result, row, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (!req.session.host) {
      return next(new Error('请先选择待配置的主机'));
    }
    res.locals.mode = parseInt(req.params.mode);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 23
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        res.locals.mode_1_disabled = true;
        res.locals.mode_2_disabled = true;
        res.locals.mode_3_disabled = true;
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
          });
          client.query("select mode, disabled from pay_i_091 where host = $1", [req.session.host.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 33
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _i, _len, _ref;
          if (err) {
            done(client);
            return next(err);
          }
          _ref = result.rows;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            row = _ref[_i];
            switch (row.mode) {
              case 1:
                res.locals.mode_1_disabled = row.disabled;
                break;
              case 2:
                res.locals.mode_2_disabled = row.disabled;
                break;
              case 3:
                res.locals.mode_3_disabled = row.disabled;
            }
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
            });
            client.query("select * from pay_i_091 where host = $1 and mode = $2 limit 1", [req.session.host.uuid, res.locals.mode], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 45
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            if (result.rows.length === 0) {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
                });
                client.query("insert into pay_i_091 ( uuid, host, mode, listen_addr, listen_port, timeout, notes ) values ( $1, $2, $3, 1, trunc(random() * 49999), 30, 'POS 009.1 接入服务配置' )", [uuid.v4(), req.session.host.uuid, res.locals.mode], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 58
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  done(client);
                  return next(err);
                }
                done();
                return res.redirect("/i/091/" + res.locals.mode);
                return __iced_k();
              });
            } else {
              done();
              return res.render('i/091', {
                record: result.rows[0]
              });
              return __iced_k();
            }
          });
        });
      };
    })(this));
  });

  router.post('/edit/:mode', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['listen_addr', 'listen_port', 'timeout'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    res.locals.mode = parseInt(req.params.mode);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 73
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
          });
          client.query("update pay_i_091 set listen_addr = $1, listen_port = $2, timeout = $3, notes = $4 where uuid = $5 and host = $6 and mode = $7", [parseInt(req.body.listen_addr), parseInt(req.body.listen_port), parseInt(req.body.timeout), req.body.notes, req.body.uuid, req.session.host.uuid, res.locals.mode], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 85
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
            });
            client.query("notify pay_i_091, '{\n    host: \"" + req.session.host.uuid + "\",\n    uuid: \"" + req.body.uuid + "\"\n}'", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 94
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.redirect("/i/091/" + res.locals.mode);
          });
        });
      };
    })(this));
  });

  router.post('/disable/:mode', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    res.locals.mode = parseInt(req.params.mode);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 107
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
          });
          client.query("update pay_i_091 set disabled = $1 where uuid = $2 and host = $3 and mode = $4", [req.body.disabled, req.body.uuid, req.session.host.uuid, res.locals.mode], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 115
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/i/091.coffee"
            });
            client.query("notify pay_i_091, '{\n    host: \"" + req.session.host.uuid + "\",\n    uuid: \"" + req.body.uuid + "\"\n}'", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 124
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.json({
              succ: true,
              redirect: "/i/091/" + res.locals.mode
            });
          });
        });
      };
    })(this));
  });

}).call(this);
